import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class MyWebView extends StatefulWidget {
  final String url;
  final String statusBarColor;
  final String title;
  final bool hideAppBar;
  final bool backForbid;

  MyWebView(this.url, this.statusBarColor, this.title, this.hideAppBar,
  {this.backForbid=true});

  @override
  _MyWebViewState createState() => _MyWebViewState();
}

class _MyWebViewState extends State<MyWebView> {

//  final webview = new WebView(url, statusBarColor, title, hideAppBar, backForbid);
   late WebViewController _controller;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    String statusBarColorStr =widget.statusBarColor==null?'ffffff':widget.statusBarColor;
    Color backButtonColor;
    if(statusBarColorStr == 'ffffff'){
      backButtonColor = Colors.black;
    }else{
      backButtonColor = Colors.white;
    }

    return Scaffold(
      body: Column(
        children: [
            _appBar(Color(int.parse('0xff'+statusBarColorStr)), backButtonColor),
          Expanded(
              child: WebView(
                initialUrl: widget.url,
                gestureNavigationEnabled:true,
                javascriptMode: JavascriptMode.unrestricted,
                onWebViewCreated: (controller){
                  _controller = controller;
                },
                onPageFinished: (url){

                },
                onProgress: (int progress){
                  print('加载进度：');
                  print(progress);
                },
                  navigationDelegate: (NavigationRequest request) {
                  if (request.url.endsWith('ctrip.com/html5/')){
                    Navigator.pop(context);
                    return NavigationDecision.prevent;

                  }
                  //对于需要拦截的操作 做判断
                    if(request.url.startsWith("myapp://")) {
                      print("即将打开 ${request.url}");
                      //做拦截处理
                      //pushto....

//                      Application.push(context, request.url);
                      return NavigationDecision.prevent;
                    }

                    //不需要拦截的操作
                    return NavigationDecision.navigate;
                  }



              )
          )

        ],
      ),
    );
  }
  _appBar(Color backgroundColor,Color backButtonColor){
    if(widget.hideAppBar==false){
      return Container(
        color: backgroundColor,
        height: 30,

      );
    }

    return Container(
      color: backgroundColor,
      padding: EdgeInsets.only(left: 0,top: 40,right: 0,bottom: 10),
      child: FractionallySizedBox(
      widthFactor:1 ,
      child: Stack(
        children: [
           GestureDetector(
             child: Container(
             margin: EdgeInsets.only(left: 10),
               child: Icon(Icons.close,color: backButtonColor,size: 26,),
           ),
             onTap: (){
               print('点击返回webview');
               Navigator.pop(context);
             },
           ),
          Positioned(
            left: 0,
            right: 0,
            child: Center(child: Text(widget.title==null?'':widget.title,
              style: TextStyle(color: backButtonColor,fontSize: 20),
            ),
            ),
          ),
        ],
      ),
    ),);
  }
}
